package com.example.mywanandroid.manager;

import android.app.Activity;

import java.lang.ref.WeakReference;

public class MyActivityManager {
    private static MyActivityManager instance = null;
    /**
     * 若引用当前Activity
     */
    private WeakReference<Activity> sCurrentActivityWeakRef;

    private MyActivityManager() {

    }

    public static MyActivityManager getInstance() {
        if (instance == null) {
            synchronized (MyActivityManager.class) {
                if (instance == null) {
                    instance = new MyActivityManager();
                }
            }
        }
        return instance;
    }

    /**
     * 获取当前activity
     * @return
     */
    public Activity getCurrentActivity(){
        Activity currentActivity = null;
        if (sCurrentActivityWeakRef != null) {
            currentActivity = sCurrentActivityWeakRef.get();
        }
        return currentActivity;
    }

    /**
     * 保存activity
     */
    public void setCurrentActivity(Activity activity){
        sCurrentActivityWeakRef = new WeakReference<Activity>(activity);
    }

}
